from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response

from day1 import settings
from .models import Book,Author

# Create your views here.

class AuthorInfo(APIView):

    def get(self,requset):

        author=Author.objects.all()

        # print(author)
        list=[]
        for i in author:
            list.append({
                'id':i.id,
                'name':i.name
            })

        return Response({
            'code':200,
            'msg':'获取作者成功',
            'data':list
        })

class Upload(APIView):
    def post(self,request):
        #  FILES;获取前端图片
        files=request.FILES.get('file')
        print('>>>>>>>>>>>>',files)
        # files.file 是字节流 name 是名字
        print('>>>>>>>>>>>>',files.name,files.file)
        # 图片写入位置
        file_dir=settings.STATICFILES_DIRS[0]
        print("图片路径前缀》》》",file_dir)
        # 拼接图片路径
        file_name=file_dir+'/'+files.name
        print('完整图片路径》》》',file_name)
        # 写入指定文件
        with open(file_name,'wb') as f:
            # 读取图片字节流写入文件
            f.write(files.file.read())
        return Response({
            'code':200,
            'msg':'上传成功',
            'files_path':file_name,
            'files_name':files.name
        })

class BookInfo(APIView):

    def get(self,request):
        data=Book.objects.all()
        books=[]
        for i in data:
            books.append({
                'id':i.id,
                'book_name':i.book_name,
                'img':i.img,
                'author':i.author.name
            })
        return Response({
            'code':200,
            'msg':'获取书籍信息成功',
            'data':books
        })

    def post(self,request):
        name=request.data.get('name')
        img=request.data.get('img')
        id=request.data.get('author_id')
        print(name,img,id)
        if not all([name,img,id]):
            return Response({
                'code':400,
                'msg':'参数错误'
            })

        if not Author.objects.get(id=id):
            return Response({
                'code':400,
                'msg':'作者不存在'
            })

        Book.objects.create(
            book_name=name,
            img=img,
            author_id=id
        )
        return Response({
            'code':200,
            'msg':'添加成功'
        })

class BookView(APIView):

    def get(self,request,id):

        book=Book.objects.get(id=id)

        return Response({
            'code':200,
            'msg':'获取书籍信息成功',
            'data':{
                'id':book.id,
                'name':book.book_name,
                'img':book.img,
                'author':book.author.id
            }
        })

    def put(self,request,id):
        name=request.data.get('book_name')
        img=request.data.get('img')
        author_id=request.data.get('author_id')

        if not all([name,img,author_id]):

            return Response({
                'code':400,
                'msg':'参数错误'
            })

        book=Book.objects.get(id=id)
        book.book_name= name
        book.img=img
        book.author_id=author_id
        book.save()
        return Response({
            'code':200,
            'msg':'修改成功'
        })

    def delete(self,request,id):
        book = Book.objects.get(id=id).delete()

        return Response({
            'code':200,
            'msg':'删除成功'
        })



